<?php
/**
 * @author k 2017/10/23
 * @describe 发货处理
 */

class erpapi_shop_matrix_yunji_request_delivery extends erpapi_shop_request_delivery
{
    /**
     * 发货请求参数
     *
     * @return void
     * @author
     **/
    protected function get_confirm_params($sdf)
    {
        $param = parent::get_confirm_params($sdf);
        // 拆单子单回写
 
       $bn_list=array();
        if($sdf['is_split'] == 1&&$sdf['split_model']==1) {
            $param['is_split']  = $sdf['is_split'];
            foreach ($sdf['delivery_items'] as $item){
                $bn_list[] = $item['shop_goods_bn'];
                $arr = array();
                $arr['bn'] = $item['shop_goods_bn'];
                $arr['num'] = $item['nums'];
                $item_list[] = $arr;
            }
        }else{
            foreach ($sdf['delivery_items'] as $item){
                $bn_list[] = $item['shop_goods_bn'];
                $arr = array();
                $arr['bn'] = $item['shop_goods_bn'];
                $arr['num'] = $item['nums'];
                $item_list[] = $arr;
            }
        }
      $param['item_list'] = json_encode($item_list);
        $param['bn_list'] = json_encode($bn_list);
        return $param;
    }



    /**
     * 发货回调
     *
     * @return void
     * @author
     **/
    public function confirm_callback($response, $callback_params)
    {

        $failApiModel = app::get('erpapi')->model('api_fail');
        $order_id        = $callback_params['order_id'];
        $err_msg = $response['err_msg'];
        $rsp             = $response['rsp'];
        $rsp=='success' ? 'succ' : $rsp;
        

        if($rsp == 'fail'){
            $fail_data = array(

                    'obj_bn'    =>  $callback_params['delivery_bn'],
                    'obj_type'  =>  'yunjidelivery',
                   
            );
            if (preg_match("/秒后再重试回传/", $err_msg)){
                
               

               $failApiModel->publish_api_fail('yunjidelivery', $fail_data, array('rsp'=>'fail','err_msg'=>$err_msg));
            }
            if (preg_match("/订单已经发货/", $err_msg)){
                $failApiModel->publish_api_fail('yunjidelivery', $fail_data, array('rsp'=>'succ'));
            }
           
        }

        if ($rsp == 'succ'){
            $fail_data = array(

                'obj_bn'    =>  $callback_params['delivery_bn'],
                'obj_type'  =>  'yunjidelivery',
               
           );

           $failApiModel->publish_api_fail('yunjidelivery', $fail_data, array('rsp'=>$rsp));
        }
        
        $rs = parent::confirm_callback($response,$callback_params);
        return $rs;
    }
}
